package com.yaodian100.ec.survey.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.dao.ObjectDao;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.survey.entity.Survey;
import com.yaodian100.ec.survey.entity.SurveyCoMaping;
import com.yaodian100.ec.survey.entity.SurveyData;
import com.yaodian100.ec.survey.service.SurveyService;

public class SurveyServiceImpl extends DomainServiceImpl<Survey> implements SurveyService {

	private ObjectDao<SurveyCoMaping> surveyCoMapingDao;
	private ObjectDao<SurveyData> surveyDataDao;

	/**
	 * 儲存问卷
	 */
	@Override
	@Transactional(readOnly = false)
	public Survey saveOrUpdateSurvey(Survey entity) throws CoreException {
		Survey survey = null;
		String content = entity.getTemplate();
		// entity.setTemplate(StringUtils.substring(com.yaodian100.core.common.utils.StringUtils.removeHTML(content, true),
		// 0,
		// 100));
		survey = getDao().saveOrUpdate(entity);
		saveToFile(survey.getId(), content);
		return survey;
	}

	/**
	 * 將问卷template写入
	 * 
	 * @return
	 */
	void saveToFile(String surveyId, String content) {
		try {
			String filename = ServerValue.getMidHtmlPath() + "survey";
			FileUtils.forceMkdir(new File(filename + File.separator));
			File file = new File(filename + File.separator + surveyId + ".txt");
			FileUtils.writeStringToFile(file, content);
		} catch (IOException e) {
			logger.info("Survey_saveToFile error :{}", e.toString());
		}
	}

	/**
	 * 將问卷template讀出
	 * 
	 * @return
	 */
	String readFromFile(String surveyId) throws IOException {
		String context = "";
		try {
			File sFile = new File(ServerValue.getMidHtmlPath() + "survey" + File.separator + surveyId + ".txt");
			logger.info("XXXXXXXX:{}", sFile.exists());
			if (sFile.exists()) {
				context = FileUtils.readFileToString(sFile);
			}
		} catch (IOException e) {
			logger.info("Survey_readFromFile error :{}", e.toString());
		}
		return context;
	}

	@Override
	@Transactional(readOnly = false)
	public SurveyData saveOrUpdateSurveyData(SurveyData surveyData) throws CoreException {
		return surveyDataDao.saveOrUpdate(surveyData);
	}

	@Override
	@Transactional(readOnly = false)
	public SurveyCoMaping saveOrUpdateSurveyCoMaping(SurveyCoMaping surveyCoMaping) throws CoreException {
		return surveyCoMapingDao.saveOrUpdate(surveyCoMaping);
	}

	@Override
	public List<SurveyData> getListSurveyData(String surveyUuid, String surveyDate) throws ParseException {
		CommonCriteria crit = new CommonCriteria();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		crit.addEq("surveyId", surveyUuid);
		crit.addGe("createDate", df.parse(surveyDate + "000000"));
		crit.addLe("createDate", df.parse(surveyDate + "235959"));
		List<SurveyData> surveyDatas = surveyDataDao.getList(crit, new String[] { "createDate asc" });
		return surveyDatas;
	}

	@Override
	public SurveyCoMaping getSurveyCoMaping(String surveyId) {
		CommonCriteria crit = new CommonCriteria();
		crit.addEq("surveyId", surveyId);
		List<SurveyCoMaping> surveyCoMapings = surveyCoMapingDao.getList(crit, null);
		if (surveyCoMapings.size() == 1) {
			return surveyCoMapings.get(0);
		} else {
			return null;
		}
	}

	@Override
	public Survey getSurvey(String surveyId) throws IOException {
		Survey survey = super.get(surveyId);
		if (null == survey) {
			return null;
		}
		String template = readFromFile(surveyId);
		if (StringUtils.isNotBlank(template)) {
			survey.setTemplate(template);
		}
		return survey;
	}

	public ObjectDao<SurveyCoMaping> getSurveyCoMapingDao() {
		return surveyCoMapingDao;
	}

	public void setSurveyCoMapingDao(ObjectDao<SurveyCoMaping> surveyCoMapingDao) {
		this.surveyCoMapingDao = surveyCoMapingDao;
	}

	public ObjectDao<SurveyData> getSurveyDataDao() {
		return surveyDataDao;
	}

	public void setSurveyDataDao(ObjectDao<SurveyData> surveyDataDao) {
		this.surveyDataDao = surveyDataDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.survey.service.SurveyService#saveOrUpdateSurvey(com.yaodian100.ec.survey.entity.Survey,
	 * com.yaodian100.ec.survey.entity.SurveyCoMaping)
	 */
	@Override
	@Transactional(readOnly = false)
	public Survey saveOrUpdateSurvey(Survey entity, SurveyCoMaping surveyCoMaping) throws CoreException {
		try {
			saveOrUpdateSurvey(entity);
			if (StringUtils.isBlank(surveyCoMaping.getSurveyId())) {
				surveyCoMaping.setSurveyId(entity.getId());
			}
			if (null != surveyCoMaping) {
				surveyCoMaping = getSurveyCoMapingDao().saveOrUpdate(surveyCoMaping);
				saveToFile(entity.getId(), entity.getTemplate());
			}
		} catch (Exception e) {
			logger.info("error:{}", e.toString());
		}
		return entity;
	}
}
